/**
 *@author GZY
 */

package sg.edu.nus.iss.team4ft.util;

import java.text.NumberFormat;

public class NumberUtil {
	private NumberUtil () {
    }

    /**
     * Format the given value to the number of decimal places specified.
     * @param value
     * @param decimalPlace
     * @return
     */
    public static String format(double value, int decimalPlace) {
        NumberFormat nft = NumberFormat.getInstance();

        nft.setMinimumFractionDigits(decimalPlace);
        nft.setMaximumFractionDigits(decimalPlace);

        return nft.format( value );
    }
    
    /**
     * Pads a number to the specified length with leading zeroes.
     *
     * @param num
     * @param len 
     * @return 
     */
    public static String lPad(int num, int len) {
        String s = String.valueOf(num);
        int length = s.length();
        if (length >= len)
            return s;

        StringBuffer sb = new StringBuffer();
        for (int i = length; i < len; i++)
            sb.append("0");
        sb.append( s );
        return sb.toString();
    }

    /**
     * Pads a number to the specified length with trailing zeroes.
     *
     * @param num
     * @param len 
     * @return 
     */
    public static String rPad(int num, int len) {
        StringBuffer s = new StringBuffer( String.valueOf(num) );
        int length = s.length();
        if (length >= len)
            return s.toString();

        for (int i = length; i < len; i++)
            s.append("0");
        return s.toString();
    }

}
